<template>
<div class="user" v-loading="loading">
    <div class="common-form d-s-c">
      <span>{{$t('plus.activity_product')}}</span>
      <div class="ml20">
        <el-button
          type="primary"
          size="small"
          icon="Plus"
          @click="changeProduct"
          v-auth="'/plus/advance/product/add'"
          >{{$t('plus.select_product')}}</el-button
        >
      </div>
    </div>
    <el-tabs v-model="auditStatus" @tab-change="handleClick">
      <el-tab-pane :label="$t('plus.all')" :name="0"></el-tab-pane>
      <el-tab-pane :label="$t('plus.pending_review')" :name="10"></el-tab-pane>
      <el-tab-pane :label="$t('plus.approved')" :name="20"></el-tab-pane>
      <el-tab-pane :label="$t('plus.rejected1')" :name="30"></el-tab-pane>
    </el-tabs>
    <div class="product-content advance-list">
      <div class="table-wrap">
        <el-table size="small" :data="tableData" border style="width: 100%">
          <el-table-column prop="productName" :label="$t('plus.product_name')" width="360">
            <template #default="scope">
              <div class="product-info">
                <div class="pic" style="width: 40px; height: 40px">
                  <img v-img-url="scope.row.imagePath" alt="" />
                </div>
                <div class="info">
                  <div class="name">{{scope.row.productName}}</div>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column :label="$t('plus.specification')">
            <template #default="scope">
              <span v-if="scope.row.specType == 10">{{$t('plus.single_spec')}}</span>
              <span v-if="scope.row.specType == 20">{{$t('plus.multi_spec')}}</span>
            </template>
          </el-table-column>
          <el-table-column :label="$t('plus.deposit')">
            <template #default="scope">
              {{scope.row.money}}
            </template>
          </el-table-column>
          <el-table-column :label="$t('plus.final_payment')">
            <template #default="scope">
              <span class="orange fb">{{scope.row.sku[0].productPrice - scope.row.money - scope.row.sku[0].advancePrice}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="limitNum" :label="$t('plus.purchase_limit_quantity')"></el-table-column>
          <el-table-column prop="stock" :label="$t('plus.activity_stock')"></el-table-column>
          <el-table-column prop="sort" :label="$t('plus.sort')"></el-table-column>
          <el-table-column :label="$t('plus.review_status')">
            <template #default="scope">
              <span v-if="scope.row.auditStatus == 10" class="green"
                >{{$t('plus.pending_review')}}</span
              >
              <span v-if="scope.row.auditStatus == 20" class="red">{{$t('plus.approved')}}</span>
              <span v-if="scope.row.auditStatus == 30" class="gray"
                >{{$t('plus.rejected1')}}</span
              >
            </template>
          </el-table-column>
          <el-table-column :label="$t('plus.status')">
            <template #default="scope">
              <span v-if="scope.row.status == 10" class="green">{{$t('plus.shelf_on')}}</span>
              <span v-if="scope.row.status == 20" class="gray">{{$t('plus.shelf_off')}}</span>
            </template>
          </el-table-column>
          <el-table-column fixed="right" :label="$t('plus.action')" width="90">
            <template #default="scope">
              <el-button
                v-auth="'/plus/advance/product/edit'"
                @click="editClick(scope.row.advanceProductId)"
                type="text"
                size="small"
                >{{$t('plus.edit')}}</el-button
              >
              <el-button
                v-auth="'/plus/advance/product/delete'"
                @click="deleteClick(scope.row.advanceProductId)"
                type="text"
                size="small"
                >{{$t('plus.delete')}}</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!--分页-->
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
          :current-page="curPage"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          :total="totalDataNumber"
        ></el-pagination>
      </div>
    </div>

    <!--商品选择-->
    <Product
      :isproduct="isproduct"
      :excludeIds="excludeIds"
      :islist="false"
      @closeDialog="closeProductFunc"
    ></Product>
  </div>
</template>

<script>
import AdvanceApi from "@/api/advance.js";
import Product from "@/components/product/Product.vue";
export default {
  components: {
    /*选择商品*/
    Product
  },
  data() {
    return {
      auditStatus: 0,
      /*表格数据*/
      tableData: [],
      /*一页多少条*/
      pageSize: 15,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1,
      /*是否加载完成*/
      loading: true,
      /*是否修改*/
      open_edit: false,
      /*当前编辑的对象*/
      userModel: {},
      /*是否打开选择商品*/
      isproduct: false,
      /*已有的id*/
      excludeIds: []
    };
  },
  created() {
    /*获取列表*/
    this.getTableList();
  },
  methods: {
    handleClick(e) {
      this.auditStatus = e;
      this.getTableList();
    },
    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.curPage = val;
      self.loading = true;
      self.getTableList();
    },
    /*每页多少条*/
    handleSizeChange(val) {
      this.curPage = 1;
      this.pageSize = val;
      this.getTableList();
    },
    /*获取列表*/
    getTableList() {
      let self = this;
      self.loading = true;
      let Params = {};
      Params.page = self.curPage;
      Params.list_rows = self.pageSize;
      Params.auditStatus = self.auditStatus;
      AdvanceApi.productList(Params, true).then(res => {
        self.excludeIds = res.data.excludeIds;
        self.tableData = res.data.list.records;
        self.totalDataNumber = res.data.list.total;
        self.loading = false;
      }).catch(error => {
        self.loading = false;
      });
    },
    /*打开添加*/
    changeProduct(list) {
      this.isproduct = true;
    },
    /*关闭商品选择*/
    closeProductFunc(e) {
      this.isproduct = false;
      if (e.type == "success") {
        this.addClick(e.params.product_id);
      }
    },
    /*添加商品*/
    addClick(product_id) {
      this.$router.push("/plus/advance/product/add?product_id=" + product_id);
    },
    /*编辑商品*/
    editClick(e) {
      let self = this;
      this.$router.push({
        path: "/plus/advance/product/edit",
        query: {
          advanceProductId: e
        }
      });
    },
    /* 查询*/
    onSubmit() {
      let self = this;
      let params = self.form;
      self.loading = true;
      self.getTableList();
    },
    /*删除用户*/
    deleteClick(e) {
      let self = this;
      ElMessageBox.confirm($t("plus.delete_confirm"), $t("plus.prompt"), {
        confirmButtonText: $t("plus.confirm_text"),
        cancelButtonText: $t("plus.cancel_text"),
        type: "warning"
      }).then(() => {
        self.loading = true;
        AdvanceApi.delProduct({
          advanceProductId: e
        }, true).then(data => {
          self.loading = false;
          ElMessage({
            message: data.msg,
            type: "success"
          });
          self.getTableList();
        }).catch(error => {
          self.loading = false;
        });
      }).catch(() => {
        self.loading = false;
      });
    }
  }
};
</script>

<style scoped>
.advance-list .el-input-number--mini {
  width: auto;
}
</style>

